package com.gavin.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {

	/**
	 * 关闭结果集
	 * 
	 * @param rs
	 */
	public static void closeResultSet(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭Statement
	 * 
	 * @param stat
	 */
	public static void closeStatement(Statement stat) {
		if (stat != null) {
			try {
				stat.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭连接
	 * 
	 * @param conn
	 */
	public static void closeConnection(Connection conn) {
		if (conn == null) {
			return;
		}
		try {
			if (conn.getAutoCommit() == false) {
				conn.setAutoCommit(true);
			}
		} catch (SQLException e1) {
		}
		try {
			conn.close();
		} catch (Exception e) {
		}
	}

	/**
	 * 关闭连接
	 * 
	 * @param conn
	 * @param bAutoCommit
	 */
	public static void closeConnection(Connection conn, boolean bAutoCommit) {
		if (conn == null) {
			return;
		}
		try {
			if (conn.getAutoCommit() != bAutoCommit) {
				conn.setAutoCommit(bAutoCommit);
			}
		} catch (Exception e) {
		}
		try {
			conn.close();
		} catch (Exception e) {
		}
	}
	
}
